package com.lagou.edu.authority.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lagou.edu.authority.entity.RoleResource;
import com.lagou.edu.authority.mapper.RoleResourceMapper;
import com.lagou.edu.authority.service.IRoleResourceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author lm
 * @since 2021/11/11 17:43
 */
@Service
public class RoleResourceServiceImpl extends ServiceImpl<RoleResourceMapper, RoleResource> implements IRoleResourceService {
    @Override
    public Set<Integer> queryResourceByRoleId(Integer roleId) {
        return lambdaQuery()
                .eq(RoleResource::getRoleId, roleId)
                .list()
                .stream()
                .map(RoleResource::getResourceId)
                .collect(Collectors.toSet());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeResourceByRoleIdAndResourceIds(Integer roleId, Set<Integer> resourceIds) {
        lambdaUpdate()
                .eq(RoleResource::getRoleId, roleId)
                .in(RoleResource::getResourceId, resourceIds)
                .remove();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeByRoleId(Integer roleId) {
        return lambdaUpdate()
                .eq(RoleResource::getRoleId, roleId)
                .remove();
    }
}
